//
//  NoDataView.m
//  CBT
//
//  Created by Mr.Zhang on 2018/12/5.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "NoDataView.h"

#import "UIView+Extension.h"

#import <BlocksKit+UIKit.h>

@interface NoDataView()

@property (nonatomic, strong) UIButton *clickBtton;

@end

@implementation NoDataView

-(instancetype)initWithFrame:(CGRect)frame type:(ShowType)type clickBlock:(void(^)(void))block
{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = UIColorFromRGB(0xf7f7f7);
        [self initSubViewsWithType:type clickBlock:block];
    }
    return self;
}

-(void)initSubViewsWithType:(ShowType)type clickBlock:(void(^)(void))block
{
    _showType = type;
    _imageView = [[UIImageView alloc]init];
    _imageView.image = [UIImage imageNamed:type == ShowType_NoData ? kLocalizedString(@"zanwushuju_c", nil) : kLocalizedString(@"duanwang_c", nil)];
    [self addSubview:_imageView];
    
    if (type == ShowType_NoNetWork) {
        _clickBtton = [[UIButton alloc]init];
        _clickBtton.titleLabel.font = [UIFont systemFontOfSize:17];
        [_clickBtton setTitle:kLocalizedString(@"重新加载", nil) forState:(UIControlStateNormal)];
        [_clickBtton setTitleColor:UIColorFromRGB(0x7889ff) forState:(UIControlStateNormal)];
        _clickBtton.layer.cornerRadius = 17.5;
        _clickBtton.layer.borderWidth = 0.5;
        _clickBtton.layer.borderColor = UIColorFromRGB(0x7889ff).CGColor;
        _clickBtton.layer.masksToBounds = YES;
        [_clickBtton bk_whenTapped:^{
            if (block != nil) {
                block();
            }
        }];
        [self addSubview:_clickBtton];
    }
}

-(void)setShowType:(ShowType)showType
{
    _showType = showType;
    self.clickBtton.hidden = _showType == ShowType_NoData;
    self.imageView.image = [UIImage imageNamed:_showType == ShowType_NoData ? kLocalizedString(@"zanwushuju_c", nil) : kLocalizedString(@"duanwang_c", nil)];
}

-(void)changeShowImage
{
    self.imageView.image = [UIImage imageNamed:_showType == ShowType_NoData ? kLocalizedString(@"zanwushuju_c", nil) : kLocalizedString(@"duanwang_c", nil)];
    [self.clickBtton setTitle:kLocalizedString(@"重新加载", nil) forState:(UIControlStateNormal)];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _imageView.size = CGSizeMake(316, 236);
    if (self.showType == ShowType_NoData) {
        _imageView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);
    }else{
        _imageView.centerX = CGRectGetWidth(self.bounds) / 2;
        _imageView.centerY = CGRectGetHeight(self.bounds) / 2  - 40;
        
        _clickBtton.frame = CGRectMake(58, _imageView.bottom + 28, ScreenNarrowSide - 118, 35);
    }
}

@end
